package com.jiyun.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.jwt.JWTUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiyun.feign.FeignCourse;
import com.jiyun.feign.FeignQuiz;
import com.jiyun.feign.FeignTrain;
import com.jiyun.feign.FeignUser;
import com.jiyun.model.Consumer;
import com.jiyun.model.Course;
import com.jiyun.model.Quiz;
import com.jiyun.model.User;
import com.jiyun.service.ConsumerService;
import com.jiyun.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/* *
 * @Author nxj
 * @Date 2023 08 2023/8/22 10 52
 * */
@RequestMapping("consumer")
@RestController
public class ConsumerController {


    @Autowired
    private FeignUser feignUser;

    @Autowired
    private FeignCourse feignCourse;


    @Autowired
    private ConsumerService consumerService;

    @Autowired
    private FeignTrain feignTrain;

    @Autowired
    private FeignQuiz feignQuiz;


    @GetMapping("findTrainQuizPaper")
    public ResultResopnse findTrainQuizPaper(@RequestParam("id")Integer id){
        return feignTrain.findTrainQuizPaper(id);
    }
    @GetMapping("findTrainId")
    public ResultResopnse findTrainId(@RequestParam("id")Integer id){
        return feignTrain.findTrainId(id);
    }
    @GetMapping("joinTrain")
    public ResultResopnse joinTrain(@RequestParam("id")Integer id){
        return feignTrain.joinTrain(id);
    }
//    一个培训有多少课
    @GetMapping("findTrainCourseNum")
    public ResultResopnse findTrainCourseNum(@RequestParam("id")Integer id){
        ResultResopnse trainCourseNum = feignTrain.findTrainCourseNum(id);
        List<Course> list = Convert.toList(Course.class, trainCourseNum.data);

//        一共多少
        ResultResopnse catalogSize = feignCourse.findCatalogSize();
        Integer catalog = Convert.toInt(catalogSize.data);
        double da = catalog.doubleValue();
        List<CourseVo>courseVoList=new ArrayList<>();
        for (Course course : list) {
            ResultResopnse catalogCourse = feignCourse.findCatalogCourse(course.getId());
            Integer leanSize = Convert.toInt(catalogCourse.data);
            double xiao = leanSize.doubleValue();
            CourseVo courseVo = new CourseVo();
            courseVo.setId(course.getId());
            courseVo.setCourseName(course.getCourseName());
            courseVo.setGood(course.getGood());
            courseVo.setJindu(Convert.toInt(xiao/da*100));
            courseVoList.add(courseVo);
        }

        return ResultResopnse.success(courseVoList);
    }

    @GetMapping("findTrainQuizNum")
    public ResultResopnse findTrainQuizNum(@RequestParam("id")Integer id){
        ResultResopnse trainQuizNum = feignTrain.findTrainQuizNum(id);
        return trainQuizNum;


    }
    @GetMapping("findTrainConsumer")
    public ResultResopnse findTrainConsumer(@RequestParam("id")Integer id){
        return feignTrain.findTrainConsumer(id);
    }

    @GetMapping("showTrainCourse")
    public ResultResopnse showTrainCourse(){
        return feignTrain.showTrainCourse();
    }

    @GetMapping("showTrainQuiz")
    public ResultResopnse showTrainQuiz(){
        return feignTrain.showTrainQuiz();
    }
    @PostMapping("showTrain")
    public ResultResopnse showTrain(@RequestBody TrainDto trainDto){
        return feignTrain.showTrain(trainDto);
    }

    @GetMapping("findTime")
    public ResultResopnse findTime(){
        ResultResopnse time = feignTrain.findTime();
        return time;
    }
    @GetMapping("findTrainCourse")
    public ResultResopnse findTrainCourse(){
        ResultResopnse trainCourse = feignTrain.findTrainCourse();
        return trainCourse;
    }

    @GetMapping("findTrainQuiz")
    public ResultResopnse findTrainQuiz(){
       return   feignTrain.findTrainQuiz();
    }

    @GetMapping("findQuizTime")
    public ResultResopnse findQuizTime(){
        return feignQuiz.findQuizTime();
    }
    @GetMapping("findQuizPaperNum")
    public ResultResopnse findQuizPaperNum(){
      return   feignQuiz.findQuizPaperNum();
    }
    @GetMapping("findQuizConsumer")
    public ResultResopnse findQuizConsumer(){
        return feignQuiz.findQuizConsumer();
    }

    @GetMapping("findTrainTime")
    public ResultResopnse findTrainTime(){
        return feignTrain.findTrainTime();
    }

    @GetMapping("findCatalogCourse")
    public ResultResopnse findCatalogCourse(Integer courseId){
        return feignCourse.findCatalogCourse(courseId);
    }

    @GetMapping("toAdd")
    public ResultResopnse toAdd(Integer id,Integer courseId){
        return feignCourse.toAdd(id,courseId);
    }

    @PostMapping("findCatalog")
    public ResultResopnse findCatalog(@RequestBody CatalogDto catalogDto){
        return feignCourse.findCatalog(catalogDto);
    }

    @GetMapping("findCatalogSize")
    public ResultResopnse findCatalog(){
        return feignCourse.findCatalogSize();
    }

    @GetMapping("findCourse")
    public ResultResopnse findCourse(){

        ResultResopnse course = feignCourse.findCourse();
        return course;
    }
    @GetMapping("findCourseId")
    public ResultResopnse findCourseId(Integer id){
        return feignCourse.findCourseId(id);
    }

    @GetMapping("findCourseTime")
    public ResultResopnse findCourseTime(){
        return feignCourse.findCourseTime();
    }
    @PostMapping("findCourseAll")
    public ResultResopnse findCourse(@RequestBody CourseDto courseDto){
        return feignCourse.findCourse(courseDto);
    }
    @GetMapping("findLabel")
    public ResultResopnse findLabel(){
        return feignCourse.findLabel();
    }

    @RequestMapping("addGood")
    public ResultResopnse addGood(Integer id){
       return consumerService.addGood(id);
    }

    @RequestMapping("login")
    public ResultResopnse login(@RequestBody Consumer consumer) {

        ResultResopnse login = feignUser.login(consumer);
        if(Integer.parseInt(login.code)==1) {
            System.out.println(login.data);
            return ResultResopnse.success(login.data);
        }else {
            return ResultResopnse.error();
        }

    }

    @GetMapping("findConsumerRid")
    public ResultResopnse findConsumerRid(Integer id){
        ResultResopnse consumerRid = feignUser.findConsumerRid(id);
        return consumerRid;
    }

    @GetMapping("findConsumerTid")
    public ResultResopnse findConsumerTid(Integer id){
        ResultResopnse consumerTid = feignUser.findConsumerTid(id);
        return consumerTid;
    }

    @GetMapping("findConsumerCid")
    public ResultResopnse findConsumerCid(Integer id){
        ResultResopnse cid = feignUser.findConsumerCid(id);
        return cid;
    }





}
